import React, { useState, createContext, useContext } from 'react'

const AuthContext = createContext();

export const useAuth = () => useContext(AuthContext)

const AuthProvider = ({ children }) => {
    const localUser = localStorage.getItem('user')
    const [user, setUser] = useState(localUser ? JSON.parse(localUser):{});
    const saveUser = (user) => {
        setUser(user);
        localStorage.setItem('user', JSON.stringify(user))
    }
    const deleteUser = () => {
        setUser({});
        localStorage.clear();
    }
    return (
        <AuthContext.Provider value={{ user, saveUser, deleteUser }}>
            {children}
        </AuthContext.Provider>
    )
}

export default AuthProvider